package com.shaco.heroproject.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.shaco.heroproject.R;
import com.shaco.heroproject.adapter.VideoAdapter;
import com.shaco.heroproject.entity.Results;
import com.shaco.heroproject.entity.VideoResult;
import com.shaco.heroproject.volley.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/5.
 */
public class VideoActivity extends Activity implements View.OnClickListener {
    private TextView videoTextTv;
    private ImageView videoImageView;
    private RecyclerView recyclerView;
    private Button activity_video_backBtn;
    private VideoAdapter adapter;
    private VolleySingleton queue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        initView();
        initData();

    }


    public void initView() {
        // TODO Auto-generated method stub
        videoTextTv = (TextView)findViewById(R.id.videoTextTv);
        videoImageView=(ImageView)findViewById(R.id.videoImgTv);
        activity_video_backBtn= (Button) findViewById(R.id.activity_video_backBtn);
        activity_video_backBtn.setOnClickListener(this);

        recyclerView = (RecyclerView) findViewById(
                R.id.videoRecycleView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(
                this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new VideoAdapter(this);
        recyclerView.setAdapter(adapter);
    }

    public void initData() {
        // TODO Auto-generated method stub
        queue = VolleySingleton.getVolleyIntance();
        String url = "http://lol.data.shiwan.com/videoLive/?channel=1";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // TODO Auto-generated method stub
                        Gson gson = new Gson();
                        VideoResult vr = gson.fromJson(response,
                                VideoResult.class);
                        if (vr != null) {
                            List<Results> results = vr.getResult();
                            adapter.addData(results);
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                error.printStackTrace();

            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // 添加请求头 在请求头中增加属性
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }

        };
        queue.addRequest(request);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.activity_video_backBtn:
                finish();
                break;
        }
    }
}
